package com.yida.gwj.config.controller;

import cn.hutool.core.util.ObjectUtil;
import com.yida.common.core.constant.UserConstants;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.bean.BeanMapUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.GwjConfig;
import com.yida.gwj.config.service.IGwjConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 参数配置 信息操作处理
 *
 * @author yida
 */
@RestController
@RequestMapping("/config")
@Api(tags = "公维金参数设置")
public class GwjConfigController extends BaseController {
    @Autowired
    private IGwjConfigService configService;

    /**
     * 获取参数配置列表
     */
    @RequiresPermissions("gwj:config:config:list")
    @ApiOperation("获取参数配置列表")
    @GetMapping("/list")
    public TableDataInfo list(GwjConfig config) {
        startPage();
        List<GwjConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("gwj:config:config:export")
    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, GwjConfig config) {
        List<GwjConfig> list = configService.selectConfigList(config);
        ExcelUtil<GwjConfig> util = new ExcelUtil<GwjConfig>(GwjConfig.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    @ApiOperation("根据参数编号获取详细信息")
    public AjaxResult getInfo(@PathVariable Long configId) {
        return AjaxResult.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    @ApiOperation("根据参数键名查询参数值")
    public AjaxResult getConfigKey(@PathVariable String configKey) {
        return AjaxResult.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/inner/configKey/{configKey}")
    @InnerAuth
    public R<String> innerGetConfigKey(@PathVariable String configKey) {
        return R.ok(configService.selectConfigByKey(configKey));
    }

    /**
     * 修改参数配置
     */
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/inner/edit")
    @InnerAuth
    public R innerEdit(@RequestParam("configKey") String configKey, @RequestParam("configValue") String configValue) {
        GwjConfig old = new GwjConfig();
        old.setConfigKey(configKey);
        GwjConfig config = configService.selectConfig(old);
        if (ObjectUtil.isNull(config)) {
            return R.fail();
        }
        config.setConfigValue(configValue);
        config.setUpdateBy(SecurityUtils.getUsername());
        if (configService.updateConfig(config) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 根据参数键名查询参数建值
     */
    @GetMapping(value = "/configKeyData/{configKey}")
    @ApiOperation("根据参数键名查询参数建值")
    public AjaxResult configKeyData(@PathVariable String configKey) {
        Map<String, Object> map = BeanMapUtils.createHashMap(new String[]{"configValue"}, new String[]{configService.selectConfigByKey(configKey)});
        return AjaxResult.success(map);
    }

    /**
     * 新增参数配置
     */
    @RequiresPermissions("gwj:config:config:add")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增参数配置")
    public AjaxResult add(@Validated @RequestBody GwjConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return AjaxResult.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getUsername());
        return toAjax(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @RequiresPermissions("gwj:config:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改参数配置")
    public AjaxResult edit(@Validated @RequestBody GwjConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return AjaxResult.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @RequiresPermissions("gwj:config:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    @ApiOperation("删除参数配置")
    public AjaxResult remove(@PathVariable Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @RequiresPermissions("gwj:config:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @ApiOperation("刷新参数缓存")
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache() {
        configService.resetConfigCache();
        return AjaxResult.success();
    }
}
